﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JLL.China.Web.Areas.Listings.Models;
using JLL.China.Infrastructure.Repositories;
using JLL.China.Infrastructure.Repositories.LINQ2SQL;
using System.Data.Linq;
using JLL.China.Model;
using JLL.China.Model.DTO;
using System.Web.Script.Serialization;
using JLL.China.Web.Libraries;
using System.IO;
using JLL.China.Infrastructure.Services;
using JLL.China.Web.Areas.Listings.Models;
namespace JLL.China.Web.Areas.Listings.Controllers
{
    public class AjaxController : Controller
    {

        //
        // GET: /Listings/Ajax/
        
        public ActionResult Index()
        {
            return View();
        }
        //
       
        //
        //GET /Listing/Map
        public string GetMap(int ID)
        {
            var dbc = new JLLCHINADataContext();
            var listing = (from l in dbc.Listings
                           where l.Listing_ID.Equals(ID)
                           select new { l.Latitude, l.Longitude }).ToList();
            return new JavaScriptSerializer().Serialize(this.Json(listing, JsonRequestBehavior.AllowGet));
        }
        #region "ProjectName"
        // GET : Project name
       public string  GetProjectName(string id)
       {
           
            var dbc = new JLLCHINADataContext();
            var project = (from p in dbc.ProjectNames
                           where p.LanguageFID  == 1 & p.Types.Equals(id) & p.Status==true
                           select new { p.Id, p.Value }).ToList();
            //return this.Json(project, JsonRequestBehavior.AllowGet);
            return new JavaScriptSerializer().Serialize(this.Json(project, JsonRequestBehavior.AllowGet));
       }

       public string CheckProjectName(FormCollection form)
       {
           var projectname = form["projectname"].ToString().Trim();
           var dbc = new JLLCHINADataContext();
           var pro = (from p in dbc.ProjectNames
                      where p.LanguageFID == 1 && p.Value.Equals(projectname)
                      select p).ToList();
           if (pro.Count > 0)
           {
               return "YES";
           }
           else
           {
               return "NO";
           }
       }
        
        #endregion 
       //
       // GET : GetPropertyType
       public string GetPropertyType()
       {

           var dbc = new JLLCHINADataContext();
           var project = (from p in dbc.PropertyTypes
                          where p.LanguageFID == 1
                          select new { p.PropertyType_ID, p.Name }).ToList();
           return new JavaScriptSerializer().Serialize(this.Json(project, JsonRequestBehavior.AllowGet));
       }

       //
       // GET : Get City
       public string GetCity()
       {

           var dbc = new JLLCHINADataContext();
           var project = (from c in dbc.Cities
                          where c.LanguageFID.Equals(1)
                          select new { c.CityID, c.CityName }).ToList();
           return new JavaScriptSerializer().Serialize(this.Json(project, JsonRequestBehavior.AllowGet));
       }

       //
       // GET : Get Location By City
       public string GetLocation(int id)
       {

           var dbc = new JLLCHINADataContext();
           var project = (from l in dbc.Locations
                          where l.LanguageFID == 1 && l.City_FID == id
                          select new { l.LocationID , l.LocationName }).ToList();
           return new JavaScriptSerializer().Serialize(this.Json(project, JsonRequestBehavior.AllowGet));
       }
        //Get Country
       public string GetCountry()
       {

           var dbc = new JLLCHINADataContext();
           var country = (from c in dbc.Countries
                          where c.IsActive == 1
                          select new { c.Country_ID, c.Country_EN }).ToList();
           return new JavaScriptSerializer().Serialize(this.Json(country, JsonRequestBehavior.AllowGet));
       }
     
        //
       //
       // GET : Get Subarea By Location
       public string GetSubArea(int id)
       {

           var dbc = new JLLCHINADataContext();
           var project = (from l in dbc.Subareas
                          where l.LanguageFID == 1 && l.LocationFID == id
                          select new { l.Subarea_ID, l.SubareaName }).ToList();
           return new JavaScriptSerializer().Serialize(this.Json(project, JsonRequestBehavior.AllowGet));
       }
       // GET : Get Features
       public string GetFeature()
       {

           var dbc = new JLLCHINADataContext();
           var feature = (from f in dbc.Features
                          where f.LanguageFID == 1 
                          select new { f.Features_ID, f.Features}).ToList();
           return new JavaScriptSerializer().Serialize(this.Json(feature, JsonRequestBehavior.AllowGet));
       }
       // GET : Get Facilities
       public string GetFacilities()
       {

           var dbc = new JLLCHINADataContext();
           var facilities = (from f in dbc.Facilities
                          where f.LanguageFID == 1
                          select new { f.Facilities_ID, f.Facilities }).ToList();
           return new JavaScriptSerializer().Serialize(this.Json(facilities, JsonRequestBehavior.AllowGet));
       }

       // GET : Get Agents
       public string GetAgents()
       {

           var dbc = new JLLCHINADataContext();
           var agent = (from a in dbc.Members
                          where a.GroupFID == 2 
                          select new { a.Member_ID, a.BusinessName }).ToList();
           return new JavaScriptSerializer().Serialize(this.Json(agent, JsonRequestBehavior.AllowGet));
       }

      
       public ActionResult ShowImage( int id )
        {
             var dbc = new JLLCHINADataContext();
             if (dbc.GetImageFromDB(id) != null)
             {
                 var imageData = dbc.GetImageFromDB(id).ToArray();
                 var  eximg = HelperUtility.MakeThumbnail((Byte[])imageData, 120, 90);
                 return File(eximg, "image/jpg");
             }
             else
             {
                 string filePath = Server.MapPath("~/Content/images/no_img_default.png");
                 string fileName = Path.GetFileName(filePath);
                 FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                 BinaryReader br = new BinaryReader(fs);
                var  eximg = br.ReadBytes((Int32)fs.Length);
                 return File(eximg, "image/jpg");
             }





        }
       #region "Count Item for each Module"
       public string CountItemByStatusByMoudle(string id)
       {
           string moudle = id.ToString();
           int CountyId =  HelperUtility.GetIdCountry();
           var _listingCriteria = new ListingSearchCriteria();
                    
           switch(moudle){
               //
               case "Property":
                   _listingCriteria.ListingType = "Resale";
                   _listingCriteria.CountryID = CountyId ;//China
                  break;
               case "PropertyInternational":
                   _listingCriteria.ListingType = "Resale";
                   _listingCriteria.CountryID = null;//
                     break;
               case "Project":
                   _listingCriteria.ListingType = "Project";
                   _listingCriteria.CountryID = CountyId;//China
                   break;
               case "ProjectInternational":
                   _listingCriteria.ListingType = "Project";
                   _listingCriteria.CountryID = null;//China
                   break;   
              
               default:
                   break;
           }
           //For active
           _listingCriteria.Status = 1;
          int  active = ListingService.Search(_listingCriteria).Count;
           //For draft
            _listingCriteria.Status = 2;
            int draft = ListingService.Search(_listingCriteria).Count;
           //For delete
           _listingCriteria.Status = 3;
           int delete = ListingService.Search(_listingCriteria).Count;
                   
           var objJson = new CountStatusListing[]{
                        new CountStatusListing{
                            countActive =active,
                            countDraft = draft,
                            countDelete = delete
                        }
                   };
           return new JavaScriptSerializer().Serialize(this.Json(objJson.ToList(), JsonRequestBehavior.AllowGet));
         
       }
       public string CountProjectDirectory(string id)
       {
           string moudle = id.ToString();
           int CountyId = HelperUtility.GetIdCountry();
           var _listingCriteria = new ListingSearchCriteria();

           switch (moudle)
           {
               //
               case "ProjectDirectoryLocal":
                  
                   _listingCriteria.CountryID = CountyId;//China
                   break;
               case "ProjectDirectoryInt":
               
                   _listingCriteria.CountryID = null;//
                   break;
              
               default:
                   break;
           }
           //For active
           _listingCriteria.Status = 1;
           int active = ProjectDirectoryService.Search(_listingCriteria).Count;
           //For draft
           _listingCriteria.Status = 2;
           int draft = ProjectDirectoryService.Search(_listingCriteria).Count;
           //For delete
           _listingCriteria.Status = 3;
           int delete = ProjectDirectoryService.Search(_listingCriteria).Count;

           var objJson = new CountStatusListing[]{
                        new CountStatusListing{
                            countActive =active,
                            countDraft = draft,
                            countDelete = delete
                        }
                   };
           return new JavaScriptSerializer().Serialize(this.Json(objJson.ToList(), JsonRequestBehavior.AllowGet));

       }
       #endregion
       #region "Image"
       public void ImageTask(int id)
        {
            string task = HttpContext.Request.QueryString["Task"].ToString();
            switch (task)
            {
                case "Status":
                    var media = ListingMediaService.GetListingMediaById(id);
                    if (media.IsActive == true)
                    {
                        media.IsActive = false;
                    }
                    else
                    {
                        media.IsActive = true;
                    }
                    ListingMediaService.Update(media);
                    ListingMediaService.Save();
                    break;
                case "Remove":
                    {
                        ListingMediaService.Delete(
                                new Model.ListingMedia 
                                { ID = id }
                                );
                        ListingMediaService.Save();

                        break;
                    }
                default :
                    var mediaD = ListingMediaService.GetListingMediaById(id);
                    if (mediaD.IsDefault == true)
                    {
                        mediaD.IsDefault = false;
                    }
                    else
                    {
                        mediaD.IsDefault = true;
                    }
                    ListingMediaService.Update(mediaD);
                    ListingMediaService.Save();
                    break;
            }
            HttpContext.Response.Write(id.ToString() + task);
        }
        public void SetSatusImage(int id)
        {

        }
        public void RemoveImage(int id)
        {

        }
        #endregion

        #region "Project Directory"
        public string CheckProjectDirectory(string name)
        {
            return name;
        }

        public string GetProjectDirectory(int id)
        {
            var dbc = new JLLCHINADataContext();
            var project = dbc.ProjectDirectoryItems.SingleOrDefault(x => x.ProjectNameFID.Equals(id)); 
            return new JavaScriptSerializer().Serialize(this.Json(project, JsonRequestBehavior.AllowGet));
        }

        #endregion

        #region "StreetName"
        [HttpPost]
        public string CreateStreetName(StreetModel item )
        {

            decimal? ID = StreetNameService.Add(item.ID, item.streetName.Trim(), item.streetNameCN.Trim());
            StreetNameService.Add(ID, item.streetName.Trim(), item.streetNameCN.Trim());
            return ID.ToString();
        }

        public string GetStreetName()
        {
            var dbc = new JLLCHINADataContext();
            var streetname = (from s in dbc.StreetNames
                              where s.LanguageFID == 1
                              select new { s.StreetName_ID, s.StreetName1 }).ToList();
            return new JavaScriptSerializer().Serialize(this.Json(streetname, JsonRequestBehavior.AllowGet));
          
        }
        #endregion

    }
}
